﻿using UnityEngine;
using System.Collections;
using System;

public class SingleFlyCannonBallState : State
{
    int vo = 15000;     // 초속도.
    int th = 45;        // 각도.
    float dh = 0;      // 높이.
    float dl = 0;       // 거리.
    float t = 0;         // 시간.
    float w = 0;         // 바람.

    GamePlayScene _playScene = null;

    public override void EnterState()
    {
        vo = 15000;     // 초속도.
        th = 45;        // 각도.
        dh = 0;      // 높이.
        dl = 0;       // 거리.
        t = 0;         // 시간.
        w = 0;         // 바람.

        _playScene = GameCommon.GetPlayScene();
    }

    public override void UpdateState(float fDT)
    {
        UpdateCannonBall();
    }

    public override void EndState()
    {
    }

    private void UpdateCannonBall()
    {
        double r = th * Math.PI / 180;            // 각도를 라디안으로 변환.

        Transform transform = _playScene._cannonBall.transform;
        Vector3 originPos = _playScene._cannonBall.originPos;

        dh = (float)(vo * Math.Sin(r) * t - 4.9 * 100f * t * t);                // vo.sin(th).t - 1/2.G.t**2.
        dl = (float)(vo * Math.Cos(r) * t + w * t * t);                               // 거리.     

        t += Time.deltaTime * 5;

        transform.localPosition = new Vector3(originPos.x + dl / 500, originPos.y + dh / 500, originPos.z);

        

        GamePlayScene playScene = GameCommon.GetPlayScene();
        
        if (_playScene._cannonBall != null)
        {
            Vector2 colPixel = Vector2.zero;

            if (WorldmapDataMgr.CollisionPixel(0, transform.localPosition, transform.localScale.x / 2, ref colPixel))
            {
                playScene._messageViewCtrl.SingleExplosion(colPixel);
            }
        }
    }
}
